﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BussinessLayer;

namespace projectQLDV
{
    public partial class frmXetLopCamTinhDoan : DevExpress.XtraEditors.XtraForm
    {
        private frmMain frmM;
        private int lctd_id;
        private DateTime ngaytochuc;
        private DataTable dths;
        private string vang;
        private string co;
        private string khong;
        public frmXetLopCamTinhDoan(frmMain frm, int id, DateTime ntc)
        {
            InitializeComponent();
            frmM = frm;
            lctd_id = id;
            ngaytochuc = ntc;
            vang = "Vắng";
            co = "Có";
            khong = "Không";
            this.Text = "Học sinh tham gia lớp cảm tình đoàn tổ chức ngày " + ngaytochuc.ToShortDateString();
            dths = new DataTable();
            formLoad();
        }

        private void formLoad()
        {
            tblHocsinh_giaovien hs = new tblHocsinh_giaovien();
            tblHocsinh_lopcamtinhdoan lctd = new tblHocsinh_lopcamtinhdoan();
            DataTable tb = new DataTable();
            try
            {
                dths = hs.SelectHocSinh();
                tb = lctd.SelectHSCuaLCTD(this.lctd_id);
                dths.Columns.Add("stt", typeof(int));
                dths.Columns.Add("select", typeof(bool));
                dths.Columns.Add("chidoan", typeof(string));
                dths.Columns.Add("duocxet", typeof(string));
                dths.Columns.Add("comat", typeof(string));
                dths.Columns.Add("gt", typeof(string));
                int i = 0;
                foreach (DataRow r in dths.Rows)
                {
                    i++;
                    r["stt"] = i;
                    r["select"] = false;
                    r["chidoan"] = r["khoi"].ToString() + r["lop"].ToString();
                    if (bool.Parse(r["gioitinh"].ToString()))
                        r["gt"] = "Nam";
                    else r["gt"] = "Nữ";
                    r["duocxet"] = khong;
                    r["comat"] = "";
                    for (int j = 0; j < tb.Rows.Count; j++)
                    {
                        if (r["id"].ToString().Trim().Equals(tb.Rows[j]["id"].ToString().Trim()))
                        {
                            
                            r["duocxet"] = "Có";
                            if (bool.Parse(tb.Rows[j]["comat"].ToString()))
                                r["comat"] = co;
                            else r["comat"] = vang;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
            this.gridControlDV.DataSource = dths;
        }

        private void frmXetLopCamTinhDoan_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmM.frmDVThamGiaLCTD_isShown = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void btnXet_Click(object sender, EventArgs e)
        {
            try
            {
                tblHocsinh_lopcamtinhdoan hs = new tblHocsinh_lopcamtinhdoan();
                for (int i = 0; i < dths.Rows.Count; i++)
                {
                    if (bool.Parse(dths.Rows[i]["select"].ToString()) && dths.Rows[i]["duocxet"].ToString().Equals(khong))
                    {
                        //insert
                        hs.hocsinh_lopcamtinhdoan(int.Parse(dths.Rows[i]["id"].ToString()), this.lctd_id, false, 1);
                        dths.Rows[i]["duocxet"] = co;
                        dths.Rows[i]["comat"] = vang;
                    }
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Error: " + ex.Message);
            }
        }

        private void btnCoMat_Click(object sender, EventArgs e)
        {
            try
            {
                tblHocsinh_lopcamtinhdoan hs = new tblHocsinh_lopcamtinhdoan();
                for (int i = 0; i < dths.Rows.Count; i++)
                {
                    if (bool.Parse(dths.Rows[i]["select"].ToString()) && dths.Rows[i]["duocxet"].ToString().Equals(co) && dths.Rows[i]["comat"].ToString().Equals(vang))
                    {
                        //update
                        hs.hocsinh_lopcamtinhdoan(int.Parse(dths.Rows[i]["id"].ToString()), this.lctd_id, true, 2);
                        dths.Rows[i]["comat"] = co;
                    }
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Error: " + ex.Message);
            }
        }

        private void btnKhongCoMat_Click(object sender, EventArgs e)
        {
            try
            {
                tblHocsinh_lopcamtinhdoan hs = new tblHocsinh_lopcamtinhdoan();
                for (int i = 0; i < dths.Rows.Count; i++)
                {
                    if (bool.Parse(dths.Rows[i]["select"].ToString()) && dths.Rows[i]["duocxet"].ToString().Equals(co) && dths.Rows[i]["comat"].ToString().Equals(co))
                    {
                        //update
                        hs.hocsinh_lopcamtinhdoan(int.Parse(dths.Rows[i]["id"].ToString()), this.lctd_id, false, 2);
                        dths.Rows[i]["comat"] = vang;
                    }
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Error: " + ex.Message);
            }
        }

        private void checkEditChonTatCa_CheckedChanged(object sender, EventArgs e)
        {
            bool check = this.checkEditChonTatCa.Checked;
            for (int i = 0; i < dths.Rows.Count; i++)
            {
                dths.Rows[i]["select"] = check;
            }
        }

        private void btnKhongDuocXet_Click(object sender, EventArgs e)
        {

            try
            {
                tblHocsinh_lopcamtinhdoan hs = new tblHocsinh_lopcamtinhdoan();
                for (int i = 0; i < dths.Rows.Count; i++)
                {
                    if (bool.Parse(dths.Rows[i]["select"].ToString()) && dths.Rows[i]["duocxet"].ToString().Equals(co))
                    {
                        //Xóa
                        hs.hocsinh_lopcamtinhdoan(int.Parse(dths.Rows[i]["id"].ToString()), this.lctd_id, false, 3);
                        dths.Rows[i]["duocxet"] = khong;
                        dths.Rows[i]["comat"] = "";
                    }
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Error: " + ex.Message);
            }
        }

        private void gridViewDV_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                dths.Rows[e.RowHandle]["select"] = !bool.Parse(dths.Rows[e.RowHandle]["select"].ToString());
            }
        }
    }
}